<?php
//新增
namespace app\blog\controller;
use cmf\controller\AdminBaseController;
use app\blog\model\GoodsModel;
use app\portal\model\PortalCategoryModel;
use think\Db;

class PrivateGoodsController extends AdminBaseController
{
    //店铺自己添加商品
    public function add()
    {
        //获取类别
        $classify=Db::name('GoodsClassify')->where(array('status'=>1))->select();
        //获取规格
        $format=DB::name('format')->select()->toArray();
        $this->assign('format',$format);
        $this->assign("classify", $classify);
        return $this->fetch();
    }
    public function addPost(){
        $uid=cmf_get_current_admin_id();
        $uinfo=get_operator_data($uid);
        if ($this->request->isPost()) {
            $res  = $this->request->param();
            if (!empty($res['photo_names']) && !empty($res['photo_urls'])){
                $res['post']['more']['photos'] = [];
                foreach ($res['photo_urls'] as $key => $url){
                    $photoUrl = cmf_asset_relative_url($url);
                    array_push($res['post']['more']['photos'], ["url" => $photoUrl, "name" => $res['photo_names'][$key]]);
                }
            }
            if(empty($res['g_goodsname'])){
                $this->error("商品名称不能为空！");
                exit;
            }
            if(empty($res['g_price'])){
                $this->error("您还没有填写价格！");
                exit;
            }           
            $format=Db::name('format')->where(array('id'=>$res['g_format']))->find();
            $res['g_format']=$format['format'];
            $res['storeid']=$uinfo['store_id'];
            $res['g_status']=-1;
            if(isset($res['photo_urls'][0])){
                $res['g_pic']=$res['photo_urls'][0];
            }
            $res['g_ctime']=time();
            $times = date("His",time());
            $rand = rand(1000,9999);
            $res['g_id'] = date("His",time()).$rand;
            unset($res['photo_urls']);
            unset($res['photo_names']);
            unset($res['post']);
            unset($res['photo_urls']);
            $re=DB::name('goods')->insert($res);
            if($re){
                $this->success("添加成功！", url("Commodity/index"));
            }else{
                $this->error("添加失败，请重新添加！", url("PrivateGoods/add"));
            }
        }
    }
    
    
}